﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Stimulsoft.Report;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Core;
using Portal.AssetTn.Model;


namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetTN_NewAsset : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AssetTN.NewAsset";
            }
        }


        public override WebPartServiceCls WebPartService
        {
            get
            {
                return AssetUtility.WebPartService;
            }
        }

        public override string Title
        {
            get
            {
                return "Thêm mới hiện vật";
            }
        }

        public override bool RunStandalone
        {
            get
            {
                return true;
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {
                string SiteId = loadParamCls.SiteId;
                Environments.CheckSessionTimeOut(true);
                Permission.CheckPermission(
                     SiteId,
                     Environments.CurrentLoginUser.LoginName,
                     Portal.AssetTn.Security.Permission.GetPermissionId(),
                     Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                     Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_InputData.ToString(),
                     Portal.AssetTn.Security.Permission.GetPermissionId(),
                     null,
                     true);



                string Subject = loadParamCls.RefTitle;
                if (string.IsNullOrEmpty(Subject))
                {
                    Subject = Title;
                }

                AssetTypeCls[] AssetTypes = AssetBll.ReadingAssetType(SiteId);
                string SelectAssetType = "<select class=drplist type=select id=drpSelectAssetType style=\"width:100%;\">\r\n";
                for (int iIndex = 0; iIndex < AssetTypes.Length; iIndex++)
                {
                    SelectAssetType +=
                        "   <option value=\"" + AssetTypes[iIndex].AssetTypeId + "\">" + AssetTypes[iIndex].AssetTypeName + "</option>\r\n";
                }
                SelectAssetType+="</select>\r\n";


                AssetStatusCls[] AssetStatus = AssetBll.ReadingAssetStatus(SiteId);
                string SelectAssetStatus =
                    "<select class=drplist Status=select id=drpSelectStatus style=\"width:100%;\">\r\n" +
                    "   <option value=\"\">Không xác định</option>\r\n";
                for (int iIndex = 0; iIndex < AssetStatus.Length; iIndex++)
                {
                    SelectAssetStatus +=
                        "   <option value=\"" + AssetStatus[iIndex].AssetStatusId + "\">" + AssetStatus[iIndex].AssetStatusName + "</option>\r\n";
                }
                SelectAssetStatus += "</select>\r\n";


                AssetNationalCls[] AssetNational = AssetBll.ReadingAssetNational(SiteId);
                string SelectAssetNational =
                    "<select class=drplist National=select id=drpSelectNational style=\"width:100%;\">\r\n"+
                    "   <option value=\"\">Không xác định</option>\r\n";
                for (int iIndex = 0; iIndex < AssetNational.Length; iIndex++)
                {
                    SelectAssetNational +=
                        "   <option value=\"" + AssetNational[iIndex].AssetNationalId + "\">" + AssetNational[iIndex].AssetNationalName + "</option>\r\n";
                }
                SelectAssetNational += "</select>\r\n";


                AssetMaterialCls[] AssetMaterial = AssetBll.ReadingAssetMaterial(SiteId);
                string SelectAssetMaterial =
                    "<select class=drplist Material=select id=drpSelectMaterial style=\"width:100%;\">\r\n" +
                    "   <option value=\"\">Không xác định</option>\r\n";
                for (int iIndex = 0; iIndex < AssetMaterial.Length; iIndex++)
                {
                    SelectAssetMaterial +=
                        "   <option value=\"" + AssetMaterial[iIndex].AssetMaterialId + "\">" + AssetMaterial[iIndex].AssetMaterialName + "</option>\r\n";
                }
                SelectAssetMaterial += "</select>\r\n";

                string RetHtml =
                        " <script language=javascript>\r\n" +
                        "   function ClientAddNewAsset()\r\n" +
                        "   {\r\n" +
                        "       SiteId='" + SiteId + "';\r\n" +
                        "       AssetTypeIndex =  document.getElementById('drpSelectAssetType').selectedIndex;\r\n" +
                        "       AssetTypeId =  document.getElementById('drpSelectAssetType').value;\r\n" +
                        "       AssetTypeName=  document.getElementById('drpSelectAssetType').options[AssetTypeIndex].text;\r\n" +

                        "       StatusIndex =  document.getElementById('drpSelectStatus').selectedIndex;\r\n" +
                        "       StatusId =  document.getElementById('drpSelectStatus').value;\r\n" +
                        "       StatusName =  document.getElementById('drpSelectStatus').options[StatusIndex].text;\r\n" +

                        "       NationalIndex =  document.getElementById('drpSelectNational').selectedIndex;\r\n" +
                        "       NationalId =  document.getElementById('drpSelectNational').value;\r\n" +
                        "       NationalName =  document.getElementById('drpSelectNational').options[NationalIndex].text;\r\n" +

                        "       MaterialIndex =  document.getElementById('drpSelectMaterial').selectedIndex;\r\n" +
                        "       MaterialId =  document.getElementById('drpSelectMaterial').value;\r\n" +
                        "       MaterialName =  document.getElementById('drpSelectMaterial').options[MaterialIndex].text;\r\n" +

                        "       Subject =  document.getElementById('txtSubject').value;\r\n" +
                        "       Description =  document.getElementById('txtDescription').value;\r\n" +

                        "       AjaxReturn  =  Portal.Gui.Web.WebParts.AssetTn.AssetTN_NewAsset.ServerSideAddNewMeterial(SiteId,Subject, Description, AssetTypeId,AssetTypeName, StatusId, StatusName, NationalId, NationalName, MaterialId, MaterialName).value;\r\n" +
                        "       if(AjaxReturn.Error)\r\n" +
                        "       {\r\n" +
                        "           alert(AjaxReturn.InfoMessage);\r\n" +
                        "           return;\r\n" +
                        "       }\r\n" +
                        "       window.open(AjaxReturn.RetUrl,'_self');\r\n" +
                        "   }\r\n" +
                        " </script>\r\n" +
                        " <div id=\"" + loadParamCls.Id + "\"> \r\n" +
                        " <input id=txtSessionId type=hidden value=\"" + SessionId + "\">\r\n" +
                        " <table class=\"il_Block\" datatable=\"0\">\r\n" +
                        " <tbody>\r\n";
                if (loadParamCls.VisibleTitle)
                {
                    RetHtml +=
                        " <tr>\r\n" +
                        "     <td class=\"ilBlockHeaderBig\" colspan=\"1\">\r\n" +
                        "       <div style=\"float:left;\">\r\n" +
                        "         <h3 class=\"ilBlockHeader\"><a class=\"ilAccAnchor\" name=\"" + loadParamCls.Id + "_blhead\" id=\"" + loadParamCls.Id + "_blhead\">" + Subject + "</a></h3> <span id=\"" + loadParamCls.Id + "_loader\"></span>\r\n" +
                        "       </div>\r\n" +
                        "     </td>\r\n" +
                        " </tr>\r\n";
                }

                RetHtml +=
                        "   <tr class=\"tblrow1\">\r\n" +
                        "       <td style=\"padding:4px;\">\r\n" +
                        "       <h1>Tạo mới hiện vật</h1>\r\n" +
                        "           <table style=\"width:100%;\">\r\n" +
                        "               <tr>\r\n" +
                        "                   <td style=\"width:150px;valign:top;\"><img style=\"width:150px;float:left;\" border=\"0\" src=\"./templates/default/images/new_large.png\"></td>\r\n" +
                        "                   <td style=\"valign:top;\">\r\n" +
                        "                       Xin vui lòng chọn loại hiện vật:" + SelectAssetType +
                        "                       <div class=tabcontent style=\"margin-top:4px;\">\r\n" +
                        "                       <table>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1 style=\"width:120px;text-align:right;margin-right:4px;\">Tên của hiện vật:</td>\r\n" +
                        "                               <td class=c2><input type=textbox class=textbox id=txtSubject></td>\r\n" +
                        "                           </tr>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1 style=\"width:120px;text-align:right;margin-right:4px;\">Mô tả hiện vật:</td>\r\n" +
                        "                               <td class=c2><textarea id=txtDescription type=textarea class=textarea style=\"width:98%;\"></textarea></td>\r\n" +
                        "                           </tr>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1 style=\"width:120px;text-align:right;margin-right:4px;\">Loại chất liệu:</td>\r\n" +
                        "                               <td class=c2>" + SelectAssetMaterial + "</td>\r\n" +
                        "                           </tr>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1 style=\"width:120px;text-align:right;margin-right:4px;\">Dân tộc:</td>\r\n" +
                        "                               <td class=c2>" + SelectAssetNational + "</td>\r\n" +
                        "                           </tr>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1 style=\"width:120px;text-align:right;margin-right:4px;\">Trạng thái:</td>\r\n" +
                        "                               <td class=c2>" + SelectAssetStatus + "</td>\r\n" +
                        "                           </tr>\r\n" +
                        "                           <tr>\r\n" +
                        "                               <td class=c1></td>\r\n" +
                        "                               <td class=c2><input type=button class=button value=\"Tạo mới hiện vật\" onclick=\"javascript:ClientAddNewAsset();\"></td>\r\n" +
                        "                           </tr>\r\n" +
                        "                       </table>\r\n" +
                        "                  </td>\r\n" +
                        "                   </td>\r\n" +
                        "               </tr>\r\n" +
                        "           </table>\r\n" +
                        "       </td>\r\n" +
                        "   </tr>\r\n" +
                        "  </tbody>\r\n" +
                        "</table>\r\n" +
                        "</div>\r\n";

                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideAddNewMeterial(
            string SiteId,
            string Subject,
            string Description,
            string AssetTypeId,
            string AssetTypeName, 
            string StatusId,
            string StatusName,
            string NationalId, 
            string NationalName, 
            string MeterialId, 
            string MeterialName)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                //doan nay tim kiem
                Environments.CheckSessionTimeOut(true);
                AssetModel oAssetModel = AssetParser.CreateInstance();

                oAssetModel.Subject = Subject;
                oAssetModel.Description = Description;
                oAssetModel.frkAssetTypeId = AssetTypeId;
                oAssetModel.AssetType = AssetTypeName;
                oAssetModel.AssetTypeName = AssetTypeName;
                oAssetModel.frkAssetStatusId = StatusId;
                oAssetModel.AssetStatusName = StatusName;
                oAssetModel.AssetStatus= StatusName;

                oAssetModel.frkAssetMaterialId = MeterialId;
                oAssetModel.AssetMaterialName = MeterialName;
                oAssetModel.Material = MeterialName;

                oAssetModel.frkAssetNationalId = NationalId;
                oAssetModel.NationalName = NationalName;
                oAssetModel.AssetNationalName = NationalName;
                string AssetId = AssetBll.AddAsset(SiteId, oAssetModel);
                oAjaxReturn.RetUrl = "v-asset.aspx?AssetId=" + AssetId.Substring(0,8)+"&mode=u";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

      

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetTN_NewAsset));
        }
    }
}
